<?
/*
############## KATEGORIJOS ##############
CREATE TABLE `f_kategorijos` (
`id` int(11) NOT NULL auto_increment,
`pavadinimas` varchar(100) NOT NULL,
`nr` int(2) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

############ FORUMUI ####################
CREATE TABLE `f_forumai` (
`id` int(10) NOT NULL auto_increment,
`kid` int(10) NOT NULL,
`tid` int(10) NOT NULL,
`pav` varchar(110) NOT NULL,
`autorius` int(10) NOT NULL,
`data` datetime NOT NULL,
`perziuru` int(10) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

############# TEMOS #####################
CREATE TABLE `f_temos` (
`id` int(10) NOT NULL auto_increment,
`kid` int(10) NOT NULL,
`pavadinimas` varchar(110) NOT NULL,
`apie` text,
`nr` int(10) NOT 0,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `f_temos` (`id`, `kid`, `pavadinimas`, `apie`, `nr`) VALUES (NULL, '3', 'Komandos', 'Diskusijos apie mIRC komandas', '1');

############ ZINUTES ####################
CREATE TABLE `f_zinutes` (
`id` int(10) NOT NULL auto_increment,
`tid` int(10) NOT NULL,
`kid` int(10) NOT NULL,
`fid` int(10) NOT NULL,
`autorius` int(1) NOT NULL,
`data` datetime NOT NULL,
`ip` varchar(11) NOT NULL,
`failas` varchar(110) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `f_zinutes` (`id`,`tid`,`kid`,`fid`,`zinute`,`autorius`,`data`) VALUES (NULL,'1','1','2','jo zjbs','1','2007-03-14 18:01:33')

# IP isgauti "INET_NTOA(ip) AS ip"
# IP ideti "`ip` = INET_ATON(".escape($test['ip']).")"

################# STRUKTURA #########################
# Kategorija	>	Temos	> Forumai	> 	Zinutes
# id			>	kid		>	tid	 	>	fid
*/

if (isset($url['k']) && isnum($url['k']) && $url['k'] > 0) { $kid = (int)$url['k']; } else { $kid = 0; } // Kategorijos ID
if (isset($url['t']) && isnum($url['t']) && $url['t'] > 0) { $tid = (int)$url['t']; } else { $tid = 0; } // Temos ID
if (isset($url['f']) && isnum($url['f']) && $url['f'] > 0) { $fid = (int)$url['f']; } else { $fid = 0; } // Forumo ID
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $pid = (int)$url['p']; } else { $pid = 0; } // Puslapiavimo ID
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; } // Trinamos zinutes ID
if (isset($url['l']) && isnum($url['l']) && $url['l'] > 0) { $lid = (int)$url['l']; } else { $lid = 0; } // Rakinamos temos ID
if (isset($url['r']) && isnum($url['r']) && $url['r'] > 0) { $rid = (int)$url['r']; } else { $rid = 0; } // Redaguojamos zinutes ID

# Skaitom kategorijas ir temas
# Darau su funkcijom tam kad kintamuju nereiktu valyt :)
if ($kid == 0) {
	lentele("Navigacija","<b>Forumas</b> &gt;");
	kategorijos();
}
# Rodom temas
elseif ($tid > 0 && $fid == 0) {
	temos($kid,$tid);
	//klaida("Klaida","Šios dalies dar nebaigiau");
}
# Ziurim postus
elseif ($fid > 0) {
	lentele("Navigacija","<a href='?id,66'><strong><span style='color:red;'>««</span> Grįžti į Forumą</strong></a>");
	zinutes($kid,$tid,$fid);
	//klaida("Klaida","Šios dalies dar net nepradėjau :)");
}

# kategoriju atvaizdavimui (pirmas puslapis)
function kategorijos() {
	global $db;
	$sql = $db->uzklausa("SELECT `id`, `pavadinimas` FROM `f_kategorijos` ORDER BY `nr`");
	if($db->rows($sql) == 0) {
		lentele("Forumas",'Nėra sukurtų kategorijų');
	} else {
		while ($row = $db->masyvas($sql)) {
			$sql1 = $db->uzklausa("SELECT `f_temos`.*,
		(SELECT Count(`f_forumai`.`id`) FROM `f_forumai` WHERE `f_forumai`.`tid` = `f_temos`.`id` AND `f_forumai`.`kid` = ".escape($row['id']).") AS `temu`,
		(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`tid` = `f_temos`.`id` AND `f_zinutes`.`kid` = ".escape($row['id']).") AS `zinuciu`
		FROM `f_temos` WHERE `f_temos`.`kid`=".escape($row['id'])." ORDER BY `f_temos`.`nr`") or die(mysql_error());
			$text = '
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
  <tr align="center">
	<td colspan="2" nowrap="nowrap" align="left" style="padding-left:18px">Pavadinimas</td>
	<td width="5%" nowrap="nowrap">Temų</td>
	<td width="6%" nowrap="nowrap">Įrašų</td>
	<td width="23%" nowrap="nowrap">Paskutinis įrašas </td>
  </tr>';
			while ($row1 = $db->masyvas($sql1)) {
				$info = $db->masyvas($db->uzklausa("SELECT `users`.`nick`, `users`.`id` AS `nick_id`, `f_zinutes`.`tid`, `f_zinutes`.`kid`, `f_zinutes`.`fid`, `f_zinutes`.`data` FROM `f_zinutes` Inner Join `users` ON `f_zinutes`.`autorius` = `users`.`id` WHERE `f_zinutes`.`tid` = ".escape($row1['id'])." AND `f_zinutes`.`kid` = ".escape($row['id'])." ORDER BY `f_zinutes`.`data` DESC LIMIT 1"));
				$text .= '
  <tr align="center">
	<td width="1%"><strong><font color="silver">&#8226;</font></strong></td>
	<td width="65%" align="left"><strong><a href="'.url("k,".$row['id'].";t,".$row1['id']."").'">'.$row1['pavadinimas'].'</a></strong><br/><span class="comment">'.$row1['apie'].'</span></td>
	<td width="5%">'.$row1['temu'].'</td>
	<td width="6%">'.$row1['zinuciu'].'</td>
	<td width="23%"><a href="?id,47;m,'.$info['nick_id'].'">'.$info['nick'].'</a><br/>'.kada($info['data']).'</td>
  </tr>';
			}
			$text .= "
</table>";
			lentele("<a href=\"#".$row['id']."\" name=\"".$row['id']."\" id=\"".$row['id']."\"><img src=\"images/icons/anchor.png\" alt=\"#\" class=\"middle\" border=\"0\"></a> ".$row['pavadinimas'],$text);
		}
	}
}

# Forumu atvaizdavimui
function temos($kid,$tid) {
	global $db;
	$sql = $db->uzklausa("SELECT `users`.`nick`, `f_forumai`.`id`, `f_forumai`.`pav`, `f_forumai`.`perziuru`, `f_forumai`.`autorius` AS `nick_id`, `f_temos`.`pavadinimas`,
(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`tid` = ".escape($tid)." AND `f_zinutes`.`kid` = ".escape($kid).") AS `zinuciu`
FROM `".T_USERS."` Inner Join `f_forumai` ON `f_forumai`.`autorius` = `users`.`id` , `f_temos`
WHERE `f_forumai`.`tid` = `f_temos`.`id` AND `f_temos`.`kid` = ".escape($kid)."");
	if ($db->rows($sql) == 0) {
		lentele("Forumas",'Nėra sukurtų temų');
	} else {
		$text = '<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
  <tr style="background-color : #cccccc;" align="center">
	<td colspan="2" nowrap="nowrap" align="left" style="padding-left:20px">Pavadinimas</td>
	<td width="5%" nowrap="nowrap">Autorius</td>
	<td width="6%" nowrap="nowrap">Įrašų</td>
	<td width="23%" nowrap="nowrap">Paskutinis įrašas </td>
  </tr>';
		$pav = "";
		while ($row = $db->masyvas($sql)) {
			$text .= '
  <tr align="center" class="sarasas">
	<td width="1%"><strong><font color="silver">&#8226;</font></strong></td>
	<td width="65%" align="left"><strong><a href="'.url("k,".$kid.";t,".$tid.";f,".$row['id']."").'">'.$row['pav'].'</a></strong></td>
	<td width="5%"><a href="?id,47;m,'.$row['nick_id'].'">'.$row['nick'].'</a></td>
	<td width="6%">'.$row['zinuciu'].'</td>
	<td width="23%"></td>
  </tr>'; if($pav != "") $pav = $row['pavadinimas'];
		}
		$text .= "\n</table>";
		lentele("Navigacija","<a href='?id,66'><strong>Forumas</strong></a> > ".$pav."");
		lentele("Įrankiai","<img src='images/forum/post.gif' alt='naujas' border='0'/>");
		lentele("Temų sarašas",$text);
		lentele("Įrankiai","<img src='images/forum/post.gif' alt='naujas' border='0'/>");
	}
}

function zinutes($kid=0,$tid=0,$fid=0) {
	global $db;
	if ($kid==0 || $tid==0 || $fid==0) { klaida("Klaida","Uptiktas pažeidimas.<h3>Jums priskaičiuotas 1 baudos balas</h3>Surinkus 5 baudos balus - <b>būsite užbanintas</b>"); redirect("?id,".$url['id'],"meta"); exit; }
	else {
		/*$sql = mysql_query("
		SELECT
		`users`.`nick`,`users`.`avatar`,`users`.`levelis`,`users`.`icq`,`users`.`msn`,`users`.`skype`,`users`.`yahoo`,`users`.`aim`,`users`.`url`,`users`.`salis`,`users`.`miestas`,`users`.`vardas`,`users`.`gim_data`,`users`.`parasas`,`users`.`taskai`,`users`.`login_data`,`users`.`balsai`,`f_zinutes`.`id`,`f_zinutes`.`zinute`,`f_zinutes`.`sypsenos`,`f_zinutes`.`autorius` AS `nick_id`,`f_zinutes`.`data`,
		(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`autorius` = `nick_id`) AS `zinuciu`,
		INET_NTOA(`f_zinutes`.`ip`) AS `ip`,`f_zinutes`.`failas`
		FROM `f_zinutes`
		Left Join `users` ON `f_zinutes`.`autorius` = `users`.`id`
		WHERE `f_zinutes`.`kid` = ".escape($kid)." AND `f_zinutes`.`tid` =  ".escape($tid)." AND `f_zinutes`.`fid` =  ".escape($fid)."
		ORDER BY `f_zinutes`.`data` ASC	LIMIT 1, 30") or die(mysql_error());*/
		$sql = mysql_query("SELECT
`users`.`nick`,
`f_zinutes`.`zinute`,
`f_zinutes`.`tid`,
`f_zinutes`.`kid`,
`f_zinutes`.`fid`,
`users`.`id` AS `nick_id`,
`f_zinutes`.`data`,
`f_zinutes`.`failas`,
(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`autorius` = `nick_id`) AS `zinuciu`,
`users`.`levelis`,
`users`.`icq`,
`users`.`msn`,
`users`.`skype`,
`users`.`yahoo`,
`users`.`url`,
`users`.`aim`,
`users`.`salis`,
`users`.`miestas`,
`users`.`vardas`,
`users`.`pavarde`,
`users`.`gim_data`,
`users`.`parasas`,
`users`.`taskai`,
`users`.`login_data`,
`users`.`avatar`,
`f_zinutes`.`sypsenos`,
INET_NTOA(`f_zinutes`.`ip`) AS `ip`
FROM
`f_zinutes`
Inner Join `users` ON `f_zinutes`.`autorius` = `users`.`id`
WHERE
`f_zinutes`.`tid` = ".escape($tid)." AND
`f_zinutes`.`kid` = ".escape($kid)." AND
`f_zinutes`.`fid` = ".escape($fid)) or die(mysql_error());
		if (mysql_affected_rows() == 0) { klaida("Klaida",'Nėra Įrašų'); } else {
			while ($row = $db->masyvas($sql)) {
				/*$title = '<span class="style1">•</span> <a href="?id,47;m,'.$row['nick_id'].'" title="header=['.$row['nick'].'] body=[<br/>
						<center><img src=\'images/levelis/'.$row['levelis'].'.gif\' alt=\''.$row['nick'].'\'/>
						<p><img src=\''.$row['avatar'].'\' width=\'100\' alt=\'kauke\'/></p></center>
						Amžius: <strong>'.amzius($row['gim_data']) .'</strong><br/>
						Taškai: <strong>'.$row['taskai'].'</strong><br/>
						Miestas: <strong>'.$row['miestas'].'</strong><br/>
						Žinutės: <strong>'.$row['zinuciu'].'</strong><br/>
						Lankėsi: <strong>'.kada($row['login_data']).'</strong><br/>
				
				<br/>] cssbody=[sarasas] cssheader=[title] fade=[on]">'.$row['nick'].'</a> <strong><a href="javascript:window.scroll(0,0)"><font size="1">↑↑</font></a></strong>';*/
				$title = '<span class="style1">•</span> <a href="?id,47;m,'.$row['nick_id'].'">'.$row['nick'].'</a> <strong><a href="javascript:window.scroll(0,0)"><font size="1">▲</font></a></strong>';
				$text = '
<table border="0" width="100%">
	<tr>
		<td valign="top" style="width:100px; border-right:1px dashed Black;">
			<center><img src=\'images/levelis/'.$row['levelis'].'.gif\' alt=\''.$row['nick'].'\'/>
			<p><img src=\''.$row['avatar'].'\' width=\'100\' alt=\'kauke\'/></p></center>
			Amžius: <strong>'.amzius($row['gim_data']) .'</strong><br/>
			Taškai: <strong>'.$row['taskai'].'</strong><br/>
			Miestas: <strong>'.$row['miestas'].'</strong><br/>
			Žinutės: <strong>'.$row['zinuciu'].'</strong><br/>
			Lankėsi: <strong>'.kada($row['login_data']).'</strong><br/>
		</td>
		<td align="left" valign="top" style="padding:10px">'.(($row['sypsenos']==1)?smile(bbcode($row['zinute'])):bbcode($row['zinute'])).'</td>
	</tr>
	<tr>
		<td colspan="2" class="sarasas" style="border-top:1px dashed Black;">
		<a href="#"><img src="images/forum/icon_pm.gif" border="0" align="left" class="middle" alt="PM"/></a>
		<a href="#"><img src="images/forum/icon_www.gif" border="0" align="left" class="middle" alt="WWW"/></a>
		<a href="#"><img src="images/forum/icon_icq.gif" border="0" align="left" class="middle" alt="ISQ"/></a>
		<a href="#"><img src="http://mystatus.skype.com/mediumicon/lenin_lt" border="0" align="left" class="middle" alt="Skype"/></a>
		<a href="#"><img src="images/forum/icon_edit.gif" border="0" align="right" class="middle" alt="Redaguoti"/></a>
		<a href="#"><img src="images/forum/icon_trinti.gif" border="0" align="right" class="middle" alt="Trinti"/></a>
		</td>
	</tr>
</table>
';
				lentele($title,$text);
			}
		}
	}
}

?>

<!--
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:10px; border-bottom: 1px solid #000000">
  <tr>
	<td width="100" rowspan="2" align="center" valign="top">
	<p><img src=\''.$row['avatar'].'\' width=\'100\' alt=\'kauke\'/></p>
	</td>
	<td height="100" colspan="2" valign="top" style="padding:5px">'.(($row['sypsenos']==1)?smile(bbcode($row['zinute'])):bbcode($row['zinute'])).'</td>
  </tr>
  
  <tr>
	<td height="5" valign="middle">Irankiai</td>
  </tr>
</table>
-->
